package Sagittarius.Manager
{
	import flash.utils.getDefinitionByName;
	
	import Sagittarius.VO.BasicVO;
	
	/**************************************************************************
	 * class			ObjectManager
	 * description		used to manage VOs
	 * @see				Sagittarius.ValueDefine.VOType
	 * @see				Sagittarius.VO.BasicVO
	 * @author			Aqua
	 *************************************************************************/
	public class ObjectManager 
	{
		
		//---------------------//
		//      Singleton      //
		//---------------------//
		
		/**************************************************************************
		 * function					instance getter
		 * description				a private function used to enforce singlton
		 * @return	ObjectManager	singleton instance of this class
		 *************************************************************************/
		public static function get instance():ObjectManager
		{
			if(null == _instance)
			{
				_instance = new ObjectManager(singleton);
			}
		
			return _instance;
		}
		
		/**************************************************************************
		 * function				singleton
		 * description			a private function used to enforce singlton
		 *************************************************************************/
		private static function singleton():void
		{
			
		}
		
		
		//---------------------//
		//     Constructor     //
		//---------------------//
		
		/**************************************************************************
		 * function				ObjectManager
		 * description			constructor
		 * @param	s			a private function used to enforce singlton
		 *************************************************************************/
		public function ObjectManager(s:Function = null):void
		{
			if (singleton != s)
			{
				throw new Error("This is a singleton class!");
			}
		}
		
		
		//-----------------//
		//     Publics     //
		//-----------------//
		
		/**************************************************************************
		 * function				addObj
		 * description			store an object such as bullet and barrage
		 * @param	id			an unique id of this object
		 * @param	type		the type of this object
		 * @param	values		an object that contains default values 
		 * 						for all member variables
		 *************************************************************************/
		public function addObj(id:String, type:String, values:Object):void
		{
			var cls:Class;
			var vo:BasicVO;
			
			if (null != _objects[id])
			{
				Logger.print(this, "Object [" + id + "] already exists!");
				return;
			}
			
			cls = getDefinitionByName(type);
			
			if (null == cls)
			{
				Logger.print(this, "Object type [" + type + "] doesn't exist!");
			}
			else
			{
				vo = new cls(values);			
				_objects[id] = vo;
			}
		}
		
		/**************************************************************************
		 * function				getObj
		 * description			get an stored object such as BulletVO and BarrageVO
		 * @param	id			the unique id of this object
		 * @return	*			the object related to the id
		 *************************************************************************/
		public function getObj(id:String):BasicVO
		{
			if (null == _objects[id])
			{
				Logger.print(this, "Object [" + id + "] doesn't exist!");
				return null;
			}
			
			return _objects[id];
		}
		
		/**************************************************************************
		 * function				hasObj
		 * description			check if an object such as bullet and barrage 
		 * 						exists in storage
		 * @param	id			an unique id of this object
		 * @return	Boolean		object exists or not
		 *************************************************************************/
		public function hasObj(id:String):Boolean
		{
			return (null != _objects[id]);
		}
		
		/**************************************************************************
		 * function				destroyObj
		 * description			destroy an stored object such as BulletVO and 
		 * 						BarrageVO
		 * @param	id			the unique id of this object
		 *************************************************************************/
		public function destroyObj(id:String):void
		{
			if (null != _objects[id])
			{
				_objects[id] = null;
			}
		}
		
		
		private static var _instance:ObjectManager;
		
		private var _objects:Object;
		
	}
	
}